package com.mspbots.core.wise.controller;

import com.mspbots.core.AbstractBaseController;
import com.mspbots.core.wise.model.TriggerEscalation;
import com.mspbots.core.wise.service.TenantRuleService;
import com.mspbots.core.wise.service.TenantService;
import com.mspbots.core.wise.service.TriggerEscalationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * com.mspbots.core.controller.TriggerEscalationController
 *
 * @author Dennis
 * @date 10:20 2019/9/26
 */
@RestController
@RequestMapping("/triggerEscalation")
public class TriggerEscalationController extends AbstractBaseController<TriggerEscalation, TriggerEscalationService> {

    @Autowired
    private TenantService tenantService;

    @Autowired
    private TenantRuleService tenantRuleService;

    @GetMapping("selectByIds/{tenantId}")
    public Callable selectByIds(@PathVariable Long tenantId, Long ruleId) {
        List<TriggerEscalation> triggerEscalations = service.selectByIds(tenantId, ruleId);
        if (triggerEscalations.size() == 0) {
            for (int i = 0; i < 3; i++) {
                TriggerEscalation triggerEscalation = new TriggerEscalation();
                triggerEscalation.setTenantId(tenantId);
                triggerEscalation.setRuleId(ruleId);
                if (i == 0) {
                    triggerEscalation.setLevel((long) 1);
                    triggerEscalation.setTimes((long) 3);
                    triggerEscalation.setTemplate("Dear {USER}, you have triggered {TRIGGERNAME} {TIMES} times this week. " +
                            "If you need help to fix this issue, please reach out to {MANAGER}. The system will notify {MANAGER} after {NEXTIMES} times.");
                } else if (i == 1) {
                    triggerEscalation.setLevel((long) 2);
                    triggerEscalation.setTimes((long) 6);
                    triggerEscalation.setTemplate("Dear {MANAGER}, {USER} has triggered {TRIGGERNAME} {TIMES} times this week. " +
                            "Please reach out to {USER} to help fix the issue. The system will notify {NEXTMANAGER} after {NEXTIMES} times.");
                } else {
                    triggerEscalation.setLevel((long) 3);
                    triggerEscalation.setTimes((long) 9);
                    triggerEscalation.setTemplate("Dear {MANAGER}, {USER} has triggered {TRIGGERNAME} {TIMES} times this week. Please reach out to {USER} to help fix the issue.");
                }
                triggerEscalations.add(triggerEscalation);
            }
        }
        service.saveOrUpdateBatch(triggerEscalations);
        return () -> service.selectByIds(tenantId, ruleId);
    }

    @PostMapping("saveOrUpdateBatch")
    public Callable saveOrUpdateBatch(@RequestBody List<TriggerEscalation> triggerEscalations) {
        return () -> this.service.saveOrUpdateBatch(triggerEscalations);
    }

    @DeleteMapping("deleteRuleEscalation")
    public Callable deleteRuleEscalation(Long tenantId, Long ruleId) {
        Map<String, Object> param = new HashMap<>();
        param.put("tenant_id", tenantId);
        param.put("rule_id", ruleId);
        return () -> service.removeByMap(param);
    }

    @PostMapping("defaultTemplate")
    public Callable defaultTemplate(Long tenantId, Long ruleId) {
        return () -> service.defaultTemplate(tenantId, ruleId, tenantRuleService);
    }
}
